package com.snow.auth.controller;

import com.snow.annotation.RequireLogin;
import com.snow.annotation.SysLog;
import com.snow.auth.component.AuthComponent;
import com.snow.auth.model.LoginUser;
import com.snow.auth.model.WechatLoginUser;
import com.snow.auth.service.SystemAuthService;
import com.snow.model.ILoginUser;
import com.snow.view.WebResponse;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;

/**
 * 系统登录接口
 *
 * @author by 13119
 */
@Slf4j
@RestController
@RequestMapping("/auth")
public class SystemAuthController {


    SystemAuthService systemAuthService;
    AuthComponent authComponent;
    final StringRedisTemplate stringRedisTemplate;

    public SystemAuthController(AuthComponent authComponent, SystemAuthService systemAuthService, StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
        log.info("SystemAuthController注入");
        this.authComponent = authComponent;
        this.systemAuthService = systemAuthService;
    }

    /**
     * 微信用户登录
     * @param params
     * @return
     */
    @RequireLogin(false)
    @PostMapping("/wechat-login")
    public WebResponse<WechatLoginUser> ownerLoginByOpenId(@Valid @RequestBody OpenIdLoginParams params) {
        WechatLoginUser wechatLoginOwner = systemAuthService.wechatLogin(params);
        return WebResponse.success(wechatLoginOwner);
    }
    @SysLog("登录")
    @PostMapping("/login")
    @RequireLogin(value = false)
    public WebResponse<Object> login(@Valid @RequestBody LoginParams params) {
        return systemAuthService.login(params);
    }

    /**
     * 登录-（使用微信，尚未开放）
     */
    @RequireLogin(false)
    @PostMapping("/login-by-wechat")
    public WebResponse<LoginUser> loginByWechat(@Valid @RequestBody OpenIdLoginParams params) {
        return WebResponse.success(systemAuthService.loginByOpenId(params.getOpenId()));
    }

    @SysLog("获取用户信息")
    @PostMapping("/get-userinfo")
    public WebResponse<ILoginUser> getUserInfo() {
        return WebResponse.success(authComponent.getCurrentUser());
    }


    @SysLog("修改密码")
    @PostMapping("/change-pwd")
    public WebResponse<Void> changePwd(@RequestBody ChangePwdParams params) {
        systemAuthService.changePwd(params.getOldPwd(), params.getNewPwd());
        return WebResponse.success();
    }

    @SysLog("退出登录")
    @RequireLogin(value = false)
    @PostMapping("/logout")
    public WebResponse<Void> logout() {
        try {
            return systemAuthService.logout();
        } catch (Exception e) {
            return WebResponse.success();
        }
    }


    @Getter
    @Setter
    public static class LoginParams {
        @NotEmpty(message = "登录账户不能为空")
        String account;
        @NotEmpty(message = "登录密码不能为空")
        String password;
    }

    @Getter
    @Setter
    public static class OpenIdLoginParams {
        @NotEmpty(message = "openid不能为空")
        String openId;
        String phoneCode;
    }

    @Data
    public static class ChangePwdParams {
        private String oldPwd;
        private String newPwd;
    }

}
